<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_subscribers.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["nw_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./modules/news/functions_subscribers.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_users.php");
$template->loadLanguageSection("news");

$interface->assignQuickLink($_SWIFT["language"]["news"], "index.php?_m=news&_a=managenews", "icon_newsitem.gif");
$interface->assignQuickLink($_SWIFT["language"]["subscribers"], "index.php?_m=news&_a=managesubscribers", "icon_subscribernotvalidated.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertnews"], "index.php?_m=news&_a=insertnews", "icon_insertnewsitem.gif");
$interface->assignQuickLink($_SWIFT["language"]["importexport"], "index.php?_m=news&_a=importexport", "icon_export.gif");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing subscribers
*/
function _subscriberFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	if ($record["validated"] == 1)
	{
		$record["validated"] = '<img src="'. $_SWIFT["themepath"] .'icon_subscribervalidated.gif" border="0" /> '.$_SWIFT["language"]["validated"];
	} else {
		$record["validated"] = '<img src="'. $_SWIFT["themepath"] .'icon_subscribernotvalidated.gif" border="0" /> '.$_SWIFT["language"]["notvalidated"];		
	}

	$record["email"] = '<a href="index.php?_m=news&_a=editsubscriber&newssubscriberid='. $record["newssubscriberid"] .'" title="'. $_SWIFT["language"]["edit"] .'">'.htmlspecialchars($record["email"]).'</a>';

	$record["dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);

	$record["options"] = '<a href="index.php?_m=news&_a=editsubscriber&newssubscriberid='. $record["newssubscriberid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=news&_a=managesubscribers&do=delete&newssubscriberid='. $record["newssubscriberid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}

/**
* Mass Action: Delete Subscribers
*/
function _maDeleteSubscribers()
{
	global $_SWIFT, $departments;

	if (!is_array($_POST["itemid"]))
	{
		return false;
	}

	deleteSubscribers($_POST["itemid"]);
}

/**
* ###############################################
* MANAGE SUBSCRIBERS
* ###############################################
*/
if ($eventaction == "managesubscribers")
{
	if ($_GET["do"] == "delete" && !empty($_GET["newssubscriberid"]))
	{
		$infomessage = $_SWIFT["language"]["dlsubscriberconfirm"];

		deleteSubscribers(array($_GET["newssubscriberid"]));
	} else if ($_POST["do"] == "insertsubscriber") {
		if (empty($_POST["email"]))
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			insertSubscriber($_POST["email"], iif($_POST["validated"]==1, true, false), 0);

			printRedirect(sprintf($_SWIFT["language"]["subinsertconfirm"], htmlspecialchars($_POST["email"])), "index.php?_m=news&_a=managesubscribers&insertsubscriber=".urlencode($_POST["email"]));
			exit;		
		}
	}
	

	if (!empty($_REQUEST["insertsubscriber"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["subinsertconfirm"], htmlspecialchars($_REQUEST["insertsubscriber"]));
	} else if (!empty($_REQUEST["updatesubscriber"])) {
		$infomessage = sprintf($_SWIFT["language"]["subupdateconfirm"], htmlspecialchars($_REQUEST["updatesubscriber"]));
	} else if ($_GET["importsub"] == 1) {
		$importresult = unserialize(base64_decode($_GET["resultdata"]));
		$importresult["totalemails"] = iif(empty($importresult["totalemails"]), "0", $importresult["totalemails"]);
		$importresult["successemails"] = iif(empty($importresult["successemails"]), "0", $importresult["successemails"]);
		$importresult["failedemails"] = iif(empty($import["failedemails"]), "0", $import["failedemails"]);
		$importresult["emaillist"] = iif(empty($import["emaillist"]), $_SWIFT["language"]["none"], nl2br($import["emaillist"]));
		$infomessage = sprintf($_SWIFT["language"]["importconfirm"], $importresult["totalemails"], $importresult["successemails"], $importresult["failedemails"], $importresult["emaillist"]);
	}
	
	
	$options["recordsperpage"] = "10";
	$options["sortby"] = "dateline";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteSubscribers";

	$options["idname"] = "newssubscriberid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["email"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'newssubscribers` WHERE email LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_subscriberFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insertsubscriber"];
	$options["toolbar"][0]["url"] = "javascript:switchDisplay('insertsubscriber');";

	$fields[0]["name"] = "email";
	$fields[0]["title"] = $_SWIFT["language"]["subemail"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "validated";
	$fields[1]["title"] = $_SWIFT["language"]["substatus"];
	$fields[1]["width"] = "150";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "dateline";
	$fields[2]["title"] = $_SWIFT["language"]["subdate"];
	$fields[2]["width"] = "180";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["news"]." &gt; ".$_SWIFT["language"]["managesubscribers"], 7);

	$interface->staffNavBar('<a href="index.php?_m=news&_a=managenews" title="'.$_SWIFT["language"]["news"].'">'.$_SWIFT["language"]["news"].'</a> &raquo; <a href="index.php?_m=news&_a=managesubscribers" title="'.$_SWIFT["language"]["managesubscribers"].'">'.$_SWIFT["language"]["managesubscribers"].'</a>', "", 7);

	$grid->start("newssubscribers", $_SWIFT["language"]["sublist"], 'SELECT * FROM `'. TABLE_PREFIX .'newssubscribers` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'newssubscribers`;', 'SELECT * FROM `'. TABLE_PREFIX .'newssubscribers` WHERE email LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	// ======= BEGIN INSERT SUBSCRIBER =======
	echo '<div id="insertsubscriber" style="DISPLAY:none;">'.SWIFT_CRLF;
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["insertsubscriber"]);

	printTextRow("email", $_SWIFT["language"]["subemail"], $_SWIFT["language"]["desc_subemail"], "text", $_POST["email"]);

	// Validation Status
	printYesNoRow("validated", $_SWIFT["language"]["subisvalidated"], $_SWIFT["language"]["desc_subisvalidated"], true);

	printSubmitRow($_SWIFT["language"]["insert"]);
	printMainTableFooter();
	addHiddenField("_m", "news");
	addHiddenField("_a", "managesubscribers");
	addHiddenField("do", "insertsubscriber");
	printFormEnd();
	echo '<BR /></div>'.SWIFT_CRLF;	


	$grid->display("newssubscribers");

	echo '<BR /><span class="smalltext">'. $_SWIFT["language"]["legend"] .'<img src="'. $_SWIFT["themepath"] .'icon_subscribervalidated.gif" border="0" /> '. $_SWIFT["language"]["validated"] .'&nbsp;&nbsp;&nbsp; <img src="'. $_SWIFT["themepath"] .'icon_subscribernotvalidated.gif" border="0" /> '. $_SWIFT["language"]["notvalidated"].'</span>';
	
	$interface->staffFooter();


/**
* ###############################################
* EDIT SUBSCRIBER
* ###############################################
*/
} else if ($eventaction == "editsubscriber") {
	$_newssubscriber = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."newssubscribers` WHERE `newssubscriberid` = '". intval($_REQUEST["newssubscriberid"]) ."';");
	if (empty($_newssubscriber["newssubscriberid"]))
	{
		trigger_error($_SWIFT["language"]["invalidsubscriber"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["email"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];			
		} else {
			updateSubscriber($_newssubscriber["newssubscriberid"], $_POST["email"]);

			if ($_POST["validated"] == 1)
			{
				validateSubscriber($_newssubscriber["newssubscriberid"]);
			}

			printRedirect(sprintf($_SWIFT["language"]["subupdateconfirm"], htmlspecialchars($_POST["email"])), "index.php?_m=news&_a=managesubscribers&updatesubscriber=".urlencode($_POST["email"]));
			exit;	
		}
	}

	$interface->staffHeader($_SWIFT["language"]["news"]." &gt; ".$_SWIFT["language"]["editsubscriber"], 7);

	$interface->staffNavBar('<a href="index.php?_m=news&_a=managesubscribers" title="'.$_SWIFT["language"]["subscribers"].'">'.$_SWIFT["language"]["subscribers"].'</a> &raquo; <a href="index.php?_m=news&_a=editsubscriber&newssubscriberid='. intval($_newssubscriber["newssubscriberid"]) .'" title="'.$_SWIFT["language"]["editsubscriber"].'">'.$_SWIFT["language"]["editsubscriber"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["subscriberdetails"]);

	printTextRow("email", $_SWIFT["language"]["subemail"], $_SWIFT["language"]["desc_subemail"], "text", $_newssubscriber["email"]);

	if ($_newssubscriber["validated"] == 0)
	{
		// Validation Status
		printYesNoRow("validated", $_SWIFT["language"]["subisvalidated"], $_SWIFT["language"]["desc_subisvalidated"], false);		
	}

	printSubmitRow($_SWIFT["language"]["update"]);
	printMainTableFooter();
	addHiddenField("_m", "news");
	addHiddenField("_a", "editsubscriber");
	addHiddenField("step", "1");
	addHiddenField("newssubscriberid", $_newssubscriber["newssubscriberid"]);
	printFormEnd();

	$template->assign("backurl", "index.php?_m=news&_a=managesubscribers");

	$interface->staffFooter();



/**
* ###############################################
* IMPORT/EXPORT
* ###############################################
*/
} else if ($eventaction == "importexport") {
	if ($_POST["step"] == "export")
	{
		exportSubscribers($_POST["filename"]);
		exit;
	} else if ($_POST["step"] == "import") {
		$importresult = importSubscribers($_POST["subscriberdata"]);

		printRedirect(sprintf($_SWIFT["language"]["importconfirm"], $importresult["totalemails"], $importresult["successemails"], $import["failedemails"], nl2br($import["emaillist"])), "index.php?_m=news&_a=managesubscribers&importsub=1&resultdata=".urlencode(base64_encode(serialize($importresult))));
		exit;
	}
	

	$interface->staffHeader($_SWIFT["language"]["news"]." &gt; ".$_SWIFT["language"]["importexport"], 7);

	$interface->staffNavBar('<a href="index.php?_m=news&_a=managesubscribers" title="'.$_SWIFT["language"]["subscribers"].'">'.$_SWIFT["language"]["subscribers"].'</a> &raquo; <a href="index.php?_m=news&_a=importexport" title="'.$_SWIFT["language"]["importexport"].'">'.$_SWIFT["language"]["importexport"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);


	// ======= BEGIN EXPORT =======
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["exportsubscriberdata"]);
	printTextRow("filename", $_SWIFT["language"]["exportfilename"], $_SWIFT["language"]["desc_exportfilename"], "text", "subscribers.csv");
	printSubmitRow($_SWIFT["language"]["export"]);
	printMainTableFooter();
	addHiddenField("_m", "news");
	addHiddenField("_a", "importexport");
	addHiddenField("step", "export");
	printFormEnd();
	// ======= END EXPORT =======

	echo "<BR />";

	// ======= BEGIN IMPORT =======
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["importsubscribers"]);

	$data = array();
	$data[0]["value"] = '<span class="smalltext">'. $_SWIFT["language"]["importdesc"] .'</span>'.SWIFT_CRLF;
	$data[0]["align"] = "left";
	$data[0]["colspan"] = "2";
	printDataRow($data);

	$data = array();
	$data[0]["value"] = '<textarea style="WIDTH:100%;" name="subscriberdata" cols="100" rows="15">'. htmlspecialchars($_POST["contents"]) .'</textarea>'.SWIFT_CRLF;
	$data[0]["align"] = "center";
	$data[0]["colspan"] = "2";
	printDataRow($data);

	printSubmitRow($_SWIFT["language"]["import"]);
	printMainTableFooter();
	addHiddenField("_m", "news");
	addHiddenField("_a", "importexport");
	addHiddenField("step", "import");
	printFormEnd();
	// ======= END IMPORT =======

	$template->assign("backurl", "index.php?_m=news&_a=managesubscribers");

	$interface->staffFooter();
}


?>